package service

import (
	_ "encoding/json"
	"fmt"
	_ "github.com/mjiulee/lego"
	"saas_sys_base/common"
	"strconv"
	"strings"
	_ "time"

	. "saas_sys_base/modules/adminSaas/models"
	. "saas_sys_base/modules/system/models"
)

type AdminSaasHomeService struct {
	// add your model here
	tbSysUser  SysUser
	tbAdminSaasMenu  AdminSaasMenu
	tbAdminSaasRole  AdminSaasRole
	tbLoginLog AdminSaasLoginLog
	//
	/*tbAppUser User
	tbDriver  Driver
	tbOrder   Order*/
}

type AdminSaasMenuVo struct {
	AdminSaasMenu
	SubMenus []AdminSaasMenu
}

func (s *AdminSaasHomeService) GetUserRoleMenu(who *common.Who, user *SysUser, profile *SysUserProfile, menuMode int) []AdminSaasMenuVo {
	menus := s.GetSystemMenu(who, menuMode)
	/*以下这段是，不用检查权限，显示每条菜单项，如果以后要检查全选，需要把这段代码删除*/
	for i, _ := range menus {
		menus[i].Check = true
		for j, _ := range menus[i].SubMenus {
			menus[i].SubMenus[j].Check = true
		}
	}
	/*saas管理端不检查权限，暂时这样，以后有精力再开启检查权限  tbroleright := AdminSaasRoleRight{}
	roleright := tbroleright.ListByRoleId(profile.RoleId)

	checkfun := func(mid int64) bool {
		for _, r := range roleright {
			if r.SId == mid {
				return true
			}
		}
		return false
	}

	for i, _ := range menus {
		menus[i].Check = checkfun(menus[i].Id)
		for j, _ := range menus[i].SubMenus {
			menus[i].SubMenus[j].Check = checkfun(menus[i].SubMenus[j].Id)
		}
	}*/

	return menus
}

func (s *AdminSaasHomeService) GetSystemMenu(who *common.Who, menuMode int) []AdminSaasMenuVo {
	topMlist := s.tbAdminSaasMenu.ListTopMenuAndMenuMode(who, menuMode)

	voList := make([]AdminSaasMenuVo, 0)
	for _, t := range topMlist {
		avo := AdminSaasMenuVo{t, nil}
		avo.SubMenus = s.tbAdminSaasMenu.ListSubMenuById(who, t.Id)
		voList = append(voList, avo)
	}
	return voList
}

/* 今日新增用户数*/
func (s *AdminSaasHomeService) GetTodayNewUser()int64 {
	return 0
	//cur := time.Now().Format("2006-01-02")
	//begin := cur + " 00:00:00"
	// 查询
	//:newUserNumb, _ := lego.GetDBEngine().Where("cdate >= ?", begin).Count(s.tbAppUser)
	//:return newUserNumb
}

/* 累计用户数*/
func (s *AdminSaasHomeService) GetTotalUser()int64 {
	return 0
	/*:newUserNumb, _ := lego.GetDBEngine().Count(s.tbAppUser)
	return newUserNumb*/
}

/* 在线司机 */
func (s *AdminSaasHomeService) GetOnLineDriver()int64 {
	//ids := helper.OnworkDriversIds()
	//return int64(len(ids))
	return 0
}

/* 司机总数 */
func (s *AdminSaasHomeService) GetTotaldriver()int64 {
	return 0
	/*:newUserNumb, _ := lego.GetDBEngine().Count(s.tbDriver)
	return newUserNumb*/
}


/* 今日订单 */
func (s *AdminSaasHomeService) GetTodayOrder()int64 {
	return 0
	/*:cur := time.Now().Format("2006-01-02")
	begin := cur + " 00:00:00"
	// 查询
	newUserNumb, _ := lego.GetDBEngine().Where("cdate >= ? && status < 9 ", begin).Count(s.tbOrder)
	return newUserNumb*/
}

/* 订单总数 */
func (s *AdminSaasHomeService) GetTotalOrder()int64 {
	return 0
	/*:newUserNumb, _ := lego.GetDBEngine().Where("status < 9 ").Count(s.tbOrder)
	return newUserNumb*/
}

/* 在线司机及位置信息 */
func (s *AdminSaasHomeService) GetDriverLocations() string {
	return ""
	/*:ids := helper.OnworkDriversIds()
	drivers := s.tbDriver.ListInIds(ids)

	for i,it := range drivers {
		loc := helper.GetDriverLocation(it.WxUid)
		drivers[i].Lat = loc.Loc.Lat
		drivers[i].Lng = loc.Loc.Lng
	}

	js ,_:= json.Marshal(drivers)
	return string(js)*/
}


func (s *AdminSaasHomeService) InitMenu() error {

	var AdminSaasUserService AdminSaasUserService

	user, _ := AdminSaasUserService.GetAdminSaasUserByAccount("admin")
	if user != nil {
		return nil
	}

	_, total, _ := AdminSaasUserService.ListAdminSaasUser("", 0, 10)
	if total > 0 {
		return nil
	}

	/*
		默认菜单：
		运营管理
	        用户管理
		配置管理

		系统管理
		    系统用户
		    角色管理
		    菜单管理
	*/

	//加入默认菜单
	//先不考虑路径前缀的问题 Config.Code.Prjprex

	menulist := []AdminSaasMenu{
		{Name: "运营管理", MType:1, Pid:0, Show:1},
		{Name: "用户管理", MType:0, Pid:-1, Show:1},

		{Name: "配置管理", MType:1, Pid:0, Show:1},


		{Name: "系统管理", MType:1, Pid:0, Show:1},
		{Name: "系统用户", MType:0, Pid:-1, Show:1, Url:"/admin/AdminSaasUser/listpage"},
		{Name: "角色管理", MType:0, Pid:-1, Show:1, Url:"/admin/AdminSaasRole/listpage"},
		{Name: "菜单管理", MType:0, Pid:-1, Show:1, Url:"/admin/AdminSaasMenu/listpage"},
	}
	var AdminSaasMenuService AdminSaasMenuService
	var menuIds []string
	var parentId int64 = 0
	for _, menu := range menulist {



		if menu.Pid == -1 {
			menu.Pid = parentId
		}
		AdminSaasMenuService.SaveAdminSaasMenu(&menu)

		if menu.Pid == 0 {
			parentId = menu.Id
		}

		menuIds = append(menuIds, strconv.FormatInt(menu.Id, 10))
	}
	menuIdStr := strings.Replace(strings.Trim(fmt.Sprint(menuIds), "[]"), " ", ",", -1)


	/*var tbAdminSaasMenu AdminSaasMenu
	var tbAdminSaasMenuParent AdminSaasMenu
	tbAdminSaasMenuParent.Name = "配置管理"
	tbAdminSaasMenuParent.MType = 1
	tbAdminSaasMenuParent.Pid = 0
	tbAdminSaasMenuParent.Show = 1

	var AdminSaasMenuService AdminSaasMenuService
	AdminSaasMenuService.SaveAdminSaasMenu(&tbAdminSaasMenuParent)

	tbAdminSaasMenu.Name = "配置菜单"
	tbAdminSaasMenu.MType = 0
	tbAdminSaasMenu.Pid = tbAdminSaasMenuParent.Id
	tbAdminSaasMenu.Show = 1
	tbAdminSaasMenu.Url = "/admin/AdminSaasMenu/listpage"
	AdminSaasMenuService.SaveAdminSaasMenu(&tbAdminSaasMenu)*/

	//加入角色
	var tbAdminSaasRole AdminSaasRole
	var AdminSaasRoleService AdminSaasRoleService
	tbAdminSaasRole.Name = "admin"
	tbAdminSaasRole.Brief = "管理员"
	//tbAdminSaasRole.AddRecode(&tbAdminSaasRole, strconv.FormatInt(tbAdminSaasMenu.Id, 10))
	//AdminSaasRoleService.SaveAdminSaasRole(&tbAdminSaasRole, fmt.Sprintf("%s,%s", strconv.FormatInt(tbAdminSaasMenu.Id, 10), strconv.FormatInt(tbAdminSaasMenuParent.Id, 10)))
	AdminSaasRoleService.SaveAdminSaasRole(&tbAdminSaasRole, menuIdStr)

	//加入系统管理用户
	var AdminSaasUser AdminSaasUser
	var AdminSaasUserProfile AdminSaasUserProfile
	AdminSaasUser.Account = "admin"
	AdminSaasUser.Password = "c92df2206dd43fa8dfa9bcb2da79d076"
	AdminSaasUser.Status = 0

	AdminSaasUserProfile.RoleId = tbAdminSaasRole.Id
	// AdminSaasUserProfile.SysUid = AdminSaasUser.Id 内部函数配置了这个关系
	AdminSaasUserService.SaveAdminSaasUser(&AdminSaasUser, &AdminSaasUserProfile)
	return nil
}